const shopDoorModel = require("../model/shopDoorModel");

//查询所有门店
module.exports.find = async function (searchObj, page) {
    // 分页查询
    let { current, size } = page;
    // 数据总条数
    let total = await shopDoorModel.countDocuments(searchObj);
    // 获取总页数: 数据总数 / 每页显示条数（注意要向上取整）
    let totalPage = Math.ceil(total / size);
    let rows = await shopDoorModel.find(searchObj)
    .populate({path:"adminId"})
    .populate({path:"commoditys"})
    .populate({path:"services"})
    .limit(parseInt(size)).skip((current - 1) * size);
    return {
        current, size, total, totalPage, rows
    }
}
//根据id查门店
module.exports.findById = async function (id) {
    return await shopDoorModel.findById(id)
}

//增加门店
module.exports.insert = async function (shopDoors) {
    return await shopDoorModel.create(shopDoors)
};



//删除门店
module.exports.delete = async function (id) {
    return await shopDoorModel.deleteOne({ _id: id })
}
//修改门店
module.exports.update = async function (id, shopDoors) {
    return await shopDoorModel.updateOne({ _id: id }, shopDoors)
}